﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WPFCouse
{
    /// <summary>
    /// CheckBoxWindow.xaml 的交互逻辑
    /// </summary>
    public partial class CheckBoxWindow : Window
    {
        public CheckBoxWindow()
        {
            InitializeComponent();
        }
        List<string> names = new List<string>();
        List<string> noSureNames = new List<string>();
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox chk = sender as CheckBox;
            names.Add(chk.Content.ToString());
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            CheckBox chk = sender as CheckBox;
            names.Remove(chk.Content.ToString());
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            string strNames = string.Join(",", names);
            lblNames.Content = strNames;
            string strSureNames = string.Join(",", noSureNames);
            lblSureNames.Content = strSureNames;
        }

        /// <summary>
        /// 单击事件  处理 选择名称的获取
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            var item = ((System.Windows.Controls.ContentControl)sender).Content as string;
            CheckBox chk = sender as CheckBox;
            if(chk.IsChecked==true)
            {
                names.Add(chk.Content.ToString());
            }
             
            else if(chk.IsChecked==false)
            {
                if (noSureNames.Contains(chk.Content.ToString()))
                    noSureNames.Remove(chk.Content.ToString());
            }
            else if(chk.IsChecked==null)
            {
                if(!noSureNames.Contains(chk.Content.ToString()))
                {
                    noSureNames.Add(chk.Content.ToString());
                    if (names.Contains(chk.Content.ToString()))
                        names.Remove(chk.Content.ToString());
                }
               
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string[] nameArr = { "李明", "张三", "王洪","代洪", "刘明" };
            for (int i = 0; i < nameArr.Length; i++)
            {
                CheckBox chk = new CheckBox();
                chk.Content = nameArr[i];
                chk.HorizontalAlignment = HorizontalAlignment.Left;
                chk.VerticalAlignment = VerticalAlignment.Top;
                chk.Margin = new Thickness(40 + i * 80, 60, 0, 0);
                chk.IsThreeState = true;
                chk.Click += CheckBox_Click;
                grid.Children.Add(chk);
            }
        }
    }
}
